package com.itheima.service.Impl;

import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import freemarker.template.utility.DateUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingdao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public Result order(Map map) throws Exception {
        //1.检查预约日期是否可以预约
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = orderSettingdao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if (orderDate == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.检查预约日期是否满人
        int reservations = orderSetting.getReservations();
        int number = orderSetting.getNumber();
        if (reservations  >= number) {
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //3.检查当前用户是否重复预约
        String telephone = (String) map.get("telephone");//获取用户录入手机号
        Member member = memberDao.findByTelephone(telephone);//调用dao，通过手机号查询会员
        //如果存在该会员，则检查当前用户是否重复预约，不存在则以该手机号自动注册
        if (member != null) {
            //检查当前用户是否重复预约
            String setmealId = (String) map.get("setmealId");//套餐id
            Date order_date = DateUtils.parseString2Date(orderDate);//预约日期
            Integer memberId = member.getId();//会员id
            //将上面三个信息封装到预约信息pojo中,以此pojo调用dao查询是否存在重复预约
            Order order = new Order(memberId, order_date, Integer.parseInt(setmealId));
            List<Order> list = orderDao.findByCondition(order);
            //如果list>0，则有重复预约
            if (list.size() > 0) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //如果没有查询到会员，则自动以录入手机号注册会员
            member = new Member();
            String name = (String) map.get("name");
            String sex = (String) map.get("sex");
            String idCard = (String) map.get("idCard");
            Date regTime = new Date();
            member.setName(name);
            member.setSex(sex);
            member.setIdCard(idCard);
            member.setRegTime(regTime);
            memberDao.add(member);
        }

        //预约成功，更新当日预约人数
        //将预约信息录入数据库
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);
        //更新预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);

        orderSettingdao.editNumberByOrderDate(orderSetting);


        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());

    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if (map != null) {
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
